﻿using System.Linq;
using System.Windows;
using System.Windows.Input;
using FM.Common;
using FM.Events;
using FM.Services;
using FM.录音.Events;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.PubSubEvents;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace FM.录音.ViewModels
{
    public class 菜单ViewModel : Vm基类<dynamic>
    {
        private bool _is全屏;

        public 菜单ViewModel(IEventAggregator 新闻部cs, IUnityContainer 人事部cs, IRegionManager 行政部cs)
        {
            新闻部 = 新闻部cs;
            人事部 = 人事部cs;
            行政部 = 行政部cs;
            log = 人事部.Resolve<ILoggerFacade>();

            退出Command = new DelegateCommand<object>(退出Action);
            选项Command = new DelegateCommand<object>(选项Action);
            全屏Command = new DelegateCommand<object>(x =>
            {
                运行状态.Is全屏 = !运行状态.Is全屏;
                新闻部.GetEvent<全屏Event>().Publish(运行状态.Is全屏);
            });
            保存布局Command = new DelegateCommand<object>(保存布局Action);

            新闻部.GetEvent<全屏Event>().Subscribe(x => Is全屏 = x, true);

            小冰.新闻部.GetEvent<进入选项Event>().Subscribe(选项Action, true);
        }


        public bool Is全屏
        {
            get { return _is全屏; }
            set { SetProperty(ref _is全屏, value); }
        }

        #region 命令

        public ICommand 全屏Command { get; set; }
        public ICommand 保存布局Command { get; set; }
        public ICommand 选项Command { get; set; }
        public ICommand 退出Command { get; set; }

        #endregion

        private void 保存布局Action(object obj)
        {
            新闻部.GetEvent<保存布局Event>().Publish(null);
        }

        private void 选项Action(object obj)
        {
            打开或者激活选项视图();
        }

        private void 打开或者激活选项视图()
        {
            log.Log("调用选项", Category.Debug, Priority.Low);

            var 窗口2 = 人事部.Resolve<Views.选项窗口>();
            var k = RegionManager.GetRegionManager(窗口2);
            if (k == null)
            {
                RegionManager.SetRegionManager(窗口2, 行政部);
                窗口2.LoadViewModel();
            }
            窗口2.ShowDialog();
        }


        private void 退出Action(object obj)
        {
            Application.Current.Shutdown();
        }
    }
}